﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace SGArchiveSystem7x
{
    
    public class MvcApplication : System.Web.HttpApplication
    {
        static string path = System.Web.HttpContext.Current.Server.MapPath("~/Content/OutPDF");
        static string path1 = System.Web.HttpContext.Current.Server.MapPath("~/Content/xls");
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ////执行判断是否是否逾期，并更新数据库，每1小时执行一次
            System.Threading.Timer timer;
            timer = new System.Threading.Timer(Work, null, 0, 1000 * 360);
            ///10分钟执行一次
            System.Threading.Timer timer2;
            timer2 = new System.Threading.Timer(Work2, null, 0, 6000 * 100);
            
        }

        private void Work2(object obj)
        {
            DateTime dt = DateTime.Now;
            string sql = "update t_archiveaj set kfzt = 1 where kfzt = 2 and ID in (select bi_archiveid from t_binfo where bi_status=0 and bi_lqdate <'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "')";
            string sql1 = "update t_binfo set bi_status=2, bi_spstatus=1, bi_v05 = '取消出库' where bi_status=0 and bi_lqdate <'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "'";

            //string sql = "update t_binfo set bi_status =2, bi_spstatus = 1, bi_v05 ='取消出库' where bi_i01 =   = (select DATEDIFF(day, bi_yjghdate, GetDate()) as d from v_binfo b " +
            //        "where bu_type = 2 and bi_status = 1 and t_binfo.bi_id = b.bi_id ) where DATEDIFF(day, bi_yjghdate, GetDate()) > 0";
            SGArchiveSystem7xDB.SGArchiveSystem7xEntities conn = new SGArchiveSystem7xDB.SGArchiveSystem7xEntities();
            conn.Database.ExecuteSqlCommand(sql);
            //string sql1 = "update T_BInfo set BI_SJGHDATE = GetDate(), BI_STATUS = 2 where BI_ID IN(select BI_ID from dbo.V_BInfo where bu_type = 1 and bi_status = 1 and DATEDIFF(day, bi_yjghdate, GetDate()) = 0)";
            conn.Database.ExecuteSqlCommand(sql1);
        }

        private void Work(object obj)
        {
            string sql = "update t_binfo set bi_yqts = (select DATEDIFF(day, bi_yjghdate, GetDate()) as d from v_binfo b " +
                    "where bu_type = 2 and bi_status = 1 and t_binfo.bi_id = b.bi_id ) where DATEDIFF(day, bi_yjghdate, GetDate()) > 0";
            SGArchiveSystem7xDB.SGArchiveSystem7xEntities conn = new SGArchiveSystem7xDB.SGArchiveSystem7xEntities();
            conn.Database.ExecuteSqlCommand(sql);
            string sql1 = "update T_BInfo set BI_SJGHDATE = GetDate(), BI_STATUS = 2 where BI_ID IN(select BI_ID from dbo.V_BInfo where bu_type = 1 and bi_status = 1 and DATEDIFF(day, bi_yjghdate, GetDate()) = 0)";
            conn.Database.ExecuteSqlCommand(sql1);



            //if (DateTime.Now.Hour >= 22 && DateTime.Now.Hour < 24)
            //{

            //    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            //    System.IO.DirectoryInfo[] ds = dir.GetDirectories();
            //    foreach (var d in ds)
            //    {
            //        try
            //        {
            //            d.Delete(true);//删除文件夹   try catch 判断是否被占用
            //        }
            //        catch { }
            //    }
            //    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path1);//获取后缀.xls/xlsx，删除 try catch 判断是否被占用
            //    System.IO.FileInfo[] ds1 = di.GetFiles("*.xlsx");
            //    foreach (var n in ds1)
            //    {
            //        try
            //        {
            //            n.Delete();
            //        }
            //        catch { }
            //    }

            //}
        }

        //保证同一次会话的SessionID不变; 不申明此方法，SessionID会发生变化
        protected void Session_Start(object sender, EventArgs e)
        { }

        /// <summary>
        /// 当某个SESSION结束后，将全局Application 里的sessionID清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_End(object sender, EventArgs e)
        {
            List<string[]> list = (List<string[]>)System.Web.HttpContext.Current.Application["SgOnline"];
            if (list != null)
            {
                foreach(var ss in list)
                {
                    if(ss[0] == Session.SessionID)
                    {
                        list.Remove(ss);
                        System.Web.HttpContext.Current.Application.Lock();
                        System.Web.HttpContext.Current.Application["SgOnline"] = list;
                        System.Web.HttpContext.Current.Application.UnLock();
                        break;
                    }
                }
            }
            ////全面更新SESSION
            Session.Abandon();
        }
    }
}
